package com.XinYun.Library.modules.Register.Service.Impl;

import com.XinYun.Library.modules.Register.Dao.Impl.AllDaoImpl;
import com.XinYun.Library.modules.Register.Dao.Impl.UserDaoImpl;
import com.XinYun.Library.modules.Register.Service.UserService;
import com.XinYun.Library.utils.FormatUtil.FormatUtil;
import com.XinYun.Library.utils.SqlUtils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class UserServiceImpl implements UserService {
    UserDaoImpl userDao = new UserDaoImpl();
    AllDaoImpl allDao = new AllDaoImpl();

    /**
     * 判断该学号是否已经被注册
     * @param sid
     * @return
     * @throws Exception
     */
    @Override
    public boolean isExist(String sid) throws Exception {
        int id = allDao.queryId(sid);
        if(id == 0){
            return false;
        }
        return true;
    }

    /**
     * 判断该新的注册信息是否已经插入数据库中
     * @param intername
     * @param sid
     * @param password
     * @param mail
     * @return
     * @throws Exception
     */
    @Override
    public boolean insert(String intername, String sid, String password, String mail) throws Exception {
        Connection connection = DruidUtil.getConnection();
        String sql = "insert into register(intername,sid,password,mail) values(?,?,?,?);";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,intername);
        ps.setString(2,sid);
        ps.setString(3,password);
        ps.setString(4,mail);
        int count = ps.executeUpdate();
        if (count != 0 && FormatUtil.isEmail(mail)) {
            return true;
        }
        DruidUtil.close(null,ps,connection);
        return false;
    }


}
